package cn.spark.study.core;

import scala.math.Ordered;

import java.io.Serializable;

/**
 * 自定义二次排序key
 *
 * @author jun.zhang6
 * @date 2020/11/6
 */
public class SecondarySortKey implements Ordered<SecondarySortKey>, Serializable {
    /**
     * 自定义需要进行排序的列
     */
    private int first;
    private int second;

    public SecondarySortKey(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public boolean $greater(SecondarySortKey that) {
        if (first > that.first) {
            return true;
        } else if (first == that.first && second > that.second) {
            return true;
        }
        return false;
    }

    @Override
    public boolean $greater$eq(SecondarySortKey that) {
        if ($greater(that)) {
            return true;
        } else if (first == that.first && second == that.second) {
            return true;
        }
        return false;
    }

    @Override
    public boolean $less(SecondarySortKey that) {
        if (first < that.first) {
            return true;
        } else if (first == that.first && second < that.second) {
            return true;
        }
        return false;
    }

    @Override
    public boolean $less$eq(SecondarySortKey that) {
        if ($less(that)) {
            return true;
        } else if (first == that.first && second == that.second) {
            return true;
        }
        return false;
    }

    @Override
    public int compare(SecondarySortKey that) {
        if (first - that.first != 0) {
            return first - that.first;
        } else {
            return second - that.second;
        }
    }


    @Override
    public int compareTo(SecondarySortKey that) {
        if (first - that.first != 0) {
            return first - that.first;
        } else {
            return second - that.second;
        }
    }
}
